package com.mdinfo.gw.door.mapper

import com.baomidou.mybatisplus.mapper.BaseMapper
import com.mdinfo.gw.door.bean.DoorCardBind
import com.mdinfo.gw.door.vo.DoorCardListQueryBindDoorVo
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select

/**
 * Created by Administrator on 2018/1/15.
 */
interface DoorCardBindMapper : BaseMapper<DoorCardBind>{

    /**根据门卡id和门id获取绑定记录*/
    @Select("""<script>
        select * from door_card_bind t
        where t.cardId = #{cardId}
        and t.doorId = #{doorId}
    </script>""")
    fun getByCardIdAndDoorId(@Param("cardId")cardId:Int,
                             @Param("doorId")doorId:Int)
            :DoorCardBind


    /**根据门卡id查询*/
    @Select("""<script>
        select * from door_card_bind t
        where t.cardId = #{cardId}
    </script>""")
    fun listByCardId(@Param("cardId")cardId:Int)
            :List<DoorCardBind>

    /**根据门卡id查询绑定的门和时间数据*/
    @Select("""<script>
        select
        d.id doorId,d.name doorName, d.devId doorDevId,
        dcb.id bindId, dcb.createTime, dcb.expireTime,
        dcb.timePickerStart,dcb.timePickerEnd,dcb.weekday
        from door_card_bind dcb
        join door d on (dcb.doorId = d.id and dcb.cardId = #{cardId})
    </script>""")
    fun listByCardDetail(@Param("cardId")cardId:Int)
            :List<DoorCardListQueryBindDoorVo>


    /**根据门卡id删除所有*/
    @Delete("""<script>
        delete from door_card_bind where cardId = #{cardId}
    </script>""")
    fun deleteAllByCardId(@Param("cardId")cardId:Int)

    /**根据门卡id和门id集合删除多条数据*/
    @Delete("""<script>
        delete from door_card_bind where cardId = #{cardId}
        <if test="doorIdList!=null and doorIdList.size>0">
            and doorId in
            <foreach collection="doorIdList" item="door_id" open="(" close=")" separator=",">
                #{door_id}
            </foreach>
        </if>
    </script>""")
    fun deleteByCardIdAndDoorIdList(@Param("cardId")cardId:Int,
                                    @Param("doorIdList")doorIdList:List<Int>)


    /**根据门id查询绑定*/
    @Select("""<script>
        select * from door_card_bind t
        where t.doorId = #{doorId}
    </script>""")
    fun listByDoorId(@Param("doorId")doorId:Int)
            :List<DoorCardBind>


    /**列表分页查询数据*/
    @Select("""<script>
        select * from door_card_bind t
        order by t.createTime desc
        limit #{start},#{num}
    </script>""")
    fun listBind(@Param("start")start:Int,
                 @Param("num")num:Int)
            :List<DoorCardBind>

    /**列表总数*/
    @Select("""<script>
        select count(1) from door_card_bind t
        order by t.createTime desc
    </script>""")
    fun countListBind():Int

}